﻿using System;
using System.ComponentModel;
using System.Configuration;
//using System.Net.Mail;
using System.Text;
using System.Web.Mail;

namespace HiTeachers.Utilities
{
    public class EmailSender
    {
        public static void SendMail(string subject, string body, string to)
        {
            //var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtp.host"], int.Parse(ConfigurationManager.AppSettings["smtp.port"]));

            //smtpClient.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["smtp.ssl"]);
            //smtpClient.UseDefaultCredentials = true;
            //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            //string username = ConfigurationManager.AppSettings["smtp.username"];
            //if (!string.IsNullOrEmpty(username))
            //{
            //    smtpClient.UseDefaultCredentials = false;
            //    smtpClient.Credentials = new System.Net.NetworkCredential(username,
            //        ConfigurationManager.AppSettings["smtp.password"]);
            //}

            //var message = new MailMessage(ConfigurationManager.AppSettings["smtp.from"], to)
            //                  {
            //                      Subject = subject,
            //                      Body = body,
            //                      IsBodyHtml = true
            //                  };

            //smtpClient.Send(message);


            MailMessage oMail = new System.Web.Mail.MailMessage();
            oMail.From = ConfigurationManager.AppSettings["smtp.from"];
            oMail.To = to;
            oMail.Subject = subject;
            oMail.BodyFormat = MailFormat.Html;   // enumeration
            oMail.Priority = MailPriority.High;   // enumeration
            oMail.BodyEncoding = Encoding.UTF8;
            oMail.Body = body;
            SmtpMail.SmtpServer = ConfigurationManager.AppSettings["smtp.host"];
            SmtpMail.Send(oMail);
            oMail = null; // free up resources
        }

        public static void SendMailAsync(string subject, string body, string to)
        {

            //var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtp.host"],
            //    int.Parse(ConfigurationManager.AppSettings["smtp.port"]))
            //        {
            //            EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["smtp.ssl"]),
            //            UseDefaultCredentials = true,
            //            DeliveryMethod = SmtpDeliveryMethod.Network
            //        };

            //string username = ConfigurationManager.AppSettings["smtp.username"];
            //if (!string.IsNullOrEmpty(username))
            //{
            //    smtpClient.UseDefaultCredentials = false;
            //    smtpClient.Credentials = new System.Net.NetworkCredential(username,
            //        ConfigurationManager.AppSettings["smtp.password"]);
            //}

            //var message = new MailMessage(ConfigurationManager.AppSettings["smtp.from"], to)
            //{
            //    Subject = subject,
            //    Body = body,
            //    IsBodyHtml = true
            //};

            //smtpClient.SendCompleted += SendCompletedCallback;
            //smtpClient.SendAsync(message, null);

            MailMessage oMail = new System.Web.Mail.MailMessage();
            oMail.From = ConfigurationManager.AppSettings["smtp.from"];
            oMail.To = to;
            oMail.Subject = subject;
            oMail.BodyFormat = MailFormat.Html;   // enumeration
            oMail.Priority = MailPriority.High;   // enumeration
            oMail.BodyEncoding = Encoding.UTF8;
            oMail.Body = body;
            SmtpMail.SmtpServer = ConfigurationManager.AppSettings["smtp.host"];
            SmtpMail.Send(oMail);
            oMail = null; // free up resources
        }

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled) Logger.Debug("Async send canceled");
            if (e.Error != null) Logger.Debug("Async send error: {0}", e.Error.Message);
            else Console.WriteLine("Message sent.");

        }
    }
}
